<template>
  <div class="app-container">
    <!--操作按钮-->
    <div style="padding-bottom: 30px;" class="operation_container">
      <ul>
        <li><el-button size="mini" @click="handleCancel">返回</el-button></li>
        <li><el-button size="mini" type="primary" @click="handleSubmit">保存</el-button></li>
      </ul>
    </div>
    <!--折叠面板-->
    <el-collapse v-model="activeNames" @change="handleChange">
      <el-collapse-item name="basicInfo">
        <template slot="title">
          <span>基本信息</span>
          <i class="header-icon el-icon-info" />
        </template>
        <el-form ref="form" :model="data" :rules="rules" :inline="true" size="mini" label-width="125px">

          <el-row :gutter="24" class="form-item-group">
            <el-col :span="6">
              <el-form-item label="托运人地址：" prop="customerAddress">
                <el-input v-model="data.customerAddress" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="供应商：" prop="customerName">
               <el-select v-model="data.serviceType" filterable clearable>
                 <el-option v-for="item in selects.materialSupplierList" :key="item.key" :label="item.label" :value="item.key" />
               </el-select>
            </el-form-item>
            </el-col>


            <el-col :span="6">
              <el-form-item label="需要调用数量：" prop="customerAddress">
                <el-input v-model="data.customerAddress" />
              </el-form-item>
            </el-col>

          </el-row>


        </el-form>
      </el-collapse-item>

    </el-collapse>
  </div>
</template>

<script>

import BasicSkuApi from '@/api/goods/BasicSkuApi'




export default {

  components: {
    BasicSpuSelector
  },
  filters: {
    categoryFilter(item) {
      const values = appConstant.categoryList.filter(value => value.key === item)
      if (values && values.length > 0) {
        return values[0].label
      } else {
        return ''
      }
    }
  },
  data() {
    return {
      activeNames: [
        'basicInfo',
        'itemInfo'],
      selects: {
         materialSupplierList: []
      },
      operation: undefined,
      data: {
        isValid: appConstant.Y


      },
      // 校验规则的名称必须和校验字段的名称一致
      rules: {
             carrierTime: [{ required: true, message: '_', trigger: 'change' }]
      },

    }
  },
  created() {
    this.initForm()
   // this.loadSelects()
  },
  methods: {
    initForm() {
         let that = this;
      const id = this.$route.params.id

        BasicSkuApi.get(id).then(response => {
          that.data = response.data
          // 将字符串转为数组

        })
       BasicSkuApi.materialSupplierList(id).then(response => {
        that.selects.materialSupplierList = response.data
      })
    },

    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // 将数组转为字符串
          if (this.data.departureIdList) {
            this.data.departure = this.data.departureIdList.join(',')
          }
          if (this.data.destinationIdList) {
            this.data.destination = this.data.destinationIdList.join(',')
          }
          if (this.operation === 'add') {
            FreightOrderApi.addAll(this.data).then(response => {
              this.$message.success('新增成功！')
              this.$router.go(-1)
            })
          } else if (this.operation === 'edit') {
            FreightOrderApi.edit(this.data).then(response => {
              this.$message.success('编辑成功！')
              this.$router.go(-1)
            })
          }
        }
      })
    },
    handleCancel() {
      this.$router.go(-1)
    },
    handleChange(val) {
      console.log(val)
    },




    handleCalShipmentExpense() {
      if (this.itemData.countPrice && this.itemData.shipByPackOrqty) {
        if (this.itemData.shipByPackOrqty === 'weight') {
          if (this.itemData.weight) {
            if (this.itemData.weight > this.data.maxWeight) {
               this.$message.warning('超过最大载重！')
               this.itemData.weight = 0;
               return
            }
            this.itemData.countQuantity = this.itemData.weight
            this.itemData.countCarriage = this.itemData.countPrice * this.itemData.weight
          }
        } else if (this.itemData.shipByPackOrqty === 'qty') {
          if (this.itemData.quantity) {
            this.itemData.countQuantity = this.itemData.quantity
            this.itemData.countCarriage = this.itemData.countPrice * this.itemData.quantity
          }
        } else if (this.itemData.shipByPackOrqty === 'pack') {
          if (this.itemData.packQty) {
            this.itemData.countQuantity = this.itemData.packQty
            this.itemData.countCarriage = this.itemData.countPrice * this.itemData.packQty
          }
        } else {
          if (this.itemData.volume) {
            if (this.itemData.volume > this.data.maxVol) {
               this.$message.warning('超过最大体积！')
                this.itemData.volume = 0;
               return
            }
            this.itemData.countQuantity = this.itemData.volume
            this.itemData.countCarriage = this.itemData.countPrice * this.itemData.volume
          }
        }
        this.itemData.amountExpense = this.itemData.countCarriage
      } else {
        this.itemData.countCarriage = 0
      }
    },
    handleCalMaterialValue() {
      if (this.itemData.unitPrice) {
        if (this.itemData.weight) {
          this.itemData.cargoValue = this.itemData.unitPrice * this.itemData.weight
        }

        this.handleCalInsuranceExpense()
      } else {
        this.itemData.materialValue = 0
      }
    },
    handleCalInsuranceExpense() {
      if (this.itemData.materialValue && this.itemData.insuranceFeeRate) {
        this.itemData.insuranceExpense = this.itemData.materialValue * this.itemData.insuranceFeeRate
      } else {
        this.itemData.insuranceExpense = 0
      }
    },
    handleLocationChange() {
      // 先清空里程
      this.data.miles = undefined
      if (this.data.departureIdList.length !== 0 && this.data.destinationIdList.length !== 0) {
        // 根据起始地和目的地，查询线路信息
        const params = {
          startLocation: this.data.departureIdList.join(','),
          endLocation: this.data.destinationIdList.join(',')
        }
        TransitRouteApi.list(params).then(response => {
          if (response.data.length > 0) {
            // 设置里程
            this.data.miles = response.data[0].milometer
            this.data.estDeliveryDays = response.data[0].estDeliveryDays
          }
        })
      }
    },
    openVehicleModelSelector() {
      this.vehicleModelSelectorVisible = true
    },
    handleVehicleModelSelected(obj) {
      this.data.vehicleModelNo = obj.id
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
